#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int, int> pii;
const int N = 510;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
char a[N][N];
int st[N][N];
int n, m;
int x1, y1, x2, y2;
void bfs()
{
    if (x1 == x2 && y1 == y2)
    {
        st[x2][y2] = 0;
        return;
    }
    memset(st, -1, sizeof(st));
    deque<pii> as;
    as.push_front({ x1,y1 });
    st[x1][y1] = 0;
    while (as.size())
    {
        auto e = as.front();
        as.pop_front();
        int q = e.first;
        int w = e.second;
        if (q == x2 && w == y2)
        {
            return;
        }
        for (int k = 0; k < 4; k++)
        {
            int x = q + dx[k];
            int y = w + dy[k];
            if (x >= 0 && x < n && y >= 0 && y < m)
            {
                char cur = a[q][w];
                char next = a[x][y];
                int s = (cur == next ? 0 : 1);
                if (st[x][y] == -1)
                {
                    st[x][y] = st[q][w] + s;
                    if (s == 0)
                    {
                        as.push_front({ x,y });
                    }
                    else
                    {
                        as.push_back({ x,y });
                    }
                }
                else if (st[q][w] + s < st[x][y])
                {
                    st[x][y] = st[q][w] + s;
                }
            }
        }
    }
}
int main()
{
    while (cin >> n >> m, n && m)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> a[i][j];
            }
        }
        cin >> x1 >> y1 >> x2 >> y2;
        bfs();
        cout << st[x2][y2] << endl;
    }
    return 0;
}